package my

import akka.actor.{ActorSystem, Props}
import akka.cluster.Cluster
import com.typesafe.config.ConfigFactory
import my.MyReceptionist.JobRequest

/**
  * Created by shichen on 2017/8/3.
  */
object MyMain extends App {
  val cfg = ConfigFactory.load()
  val sys = ActorSystem("words",cfg)

  if (sys.settings.config.getStringList("akka.cluster.roles").contains("master")) {
    //如果是master,就要在这个节点上启动JobReceptionist
    //分布式编程一定要考虑到这个程序将来会在多台机器上运行，不同机器
    //很可能运行不同的逻辑，所以需要有if判断来在正确的node上运行正确的代码
    Cluster(sys).registerOnMemberUp {
      val receptionist = sys.actorOf(Props[MyReceptionist],"receptionist")
      val task = List("this is a test", "of some very naive word counting","but what can you say", "it is what it is")
      receptionist ! JobRequest("firstjob",(1 to 20000).flatMap(i => task ++ task).toList)
      sys.actorOf(Props(new MyClusterDomainEventEventListener),
        name = "cluster-listener")
    }
  }
}
